#!/usr/bin/env pwsh
<#
.SYNOPSIS
    文语编程语言 CLI 包装脚本
    
.DESCRIPTION
    提供简化的wenyu命令来运行文语程序
    
.PARAMETER Command
    命令: run, compile, --version, --help
    
.PARAMETER File
    文语源文件路径
    
.EXAMPLE
    wenyu run hello.wy
    wenyu compile hello.wy
    wenyu --version
    wenyu --help
#>

param(
    [Parameter(Position=0)]
    [string]$Command,
    
    [Parameter(Position=1)]
    [string]$File
)

# 获取项目根目录
$projectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$cliPath = Join-Path $projectRoot "src\WenYu.CLI\WenYu.CLI.csproj"

# 如果未提供命令，显示帮助
if (-not $Command) {
    & dotnet run --project $cliPath -- --help
    exit $LASTEXITCODE
}

# 执行对应的命令
switch ($Command.ToLower()) {
    "run" {
        if (-not $File) {
            Write-Error "❌ 错误: 缺少文件参数"
            Write-Error "用法: wenyu run <file>"
            exit 1
        }
        
        # 检查文件是否存在
        if (-not (Test-Path $File)) {
            Write-Error "❌ 错误: 文件不存在: $File"
            exit 1
        }
        
        # 转换为绝对路径
        $absolutePath = Convert-Path $File
        
        Write-Host "📂 运行: $File" -ForegroundColor Cyan
        & dotnet run --project $cliPath -- run $absolutePath
        exit $LASTEXITCODE
    }
    
    "compile" {
        if (-not $File) {
            Write-Error "❌ 错误: 缺少文件参数"
            Write-Error "用法: wenyu compile <file>"
            exit 1
        }
        
        # 检查文件是否存在
        if (-not (Test-Path $File)) {
            Write-Error "❌ 错误: 文件不存在: $File"
            exit 1
        }
        
        # 转换为绝对路径
        $absolutePath = Convert-Path $File
        
        Write-Host "📂 编译: $File" -ForegroundColor Cyan
        & dotnet run --project $cliPath -- compile $absolutePath
        exit $LASTEXITCODE
    }
    
    "--version" {
        & dotnet run --project $cliPath -- --version
        exit $LASTEXITCODE
    }
    
    "--help" {
        & dotnet run --project $cliPath -- --help
        exit $LASTEXITCODE
    }
    
    "-h" {
        & dotnet run --project $cliPath -- --help
        exit $LASTEXITCODE
    }
    
    default {
        Write-Error "❌ 未知命令: $Command"
        Write-Host ""
        & dotnet run --project $cliPath -- --help
        exit 1
    }
}
